<?php

require_once(DIR_LIB."VsGui".DIR_SEP."vssmartywidget.php");

require_once(dirname(__FILE__).DIR_SEP."vsdateedit.php");

class XCartOperatorWork extends VsSmartyWidget {

	function __construct(&$page, $operatorId, $pager=NULL) {
		// Ne go zabravqj ina4e nqma $smarty Object
		parent::__construct();

		$orderBy = isset($_GET['order_by']) ? $_GET['order_by'] : '';

		if(isset($_POST['group_by'])) {
			$groupBy = $_POST['group_by'];

			if($groupBy=='operation')
				$this->_template = dirname(__FILE__).DIR_SEP."xcart_operator_work_groupby_operation.html";
			else
				$this->_template = dirname(__FILE__).DIR_SEP."xcart_operator_work.html";
		}
		else {
			$groupBy = '';
		
			$this->_template = dirname(__FILE__).DIR_SEP."xcart_operator_work.html";
		}

		$this->_wwwDir = 'templates/XCart/';

		if(isset($_POST['sub_works'])) {
			$this->procForm();
		}

		$getQuery = VsApplication::Instance()->getQuery();
		$getQuery->removeVar('order_by');
		$this->assign('getQuery', $getQuery->string());

		$page->addJsFile($this->_wwwDir."js/xcart_operator_work.js");
		$page->addJsFile($this->_wwwDir."js/vsprint.js");


		//$minDate = isset($_POST['fromVsdeDate']) ? $_POST['fromVsdeDate'] : date("Y-m-d");
		//$maxDate = isset($_POST['toVsdeDate']) ? $_POST['toVsdeDate'] : date("Y-m-d");
		$minDate = isset($_REQUEST['fromVsdeDate']) ? $_REQUEST['fromVsdeDate'] : date("Y-m-d");
		$maxDate = isset($_REQUEST['toVsdeDate']) ? $_REQUEST['toVsdeDate'] : date("Y-m-d");

		$fromDateEdit = new VsDateEdit($page, $minDate);
		$this->assign('fromDateEdit',  $fromDateEdit->show());
		$this->assign('fromVsdeDate',  $fromDateEdit->date());

		$toDateEdit = new VsDateEdit($page, $maxDate);
		$this->assign('toDateEdit',  $toDateEdit->show());
		$this->assign('toVsdeDate',  $toDateEdit->date());

		
		$opWork = JunOperatorWork::makeForView($operatorId, $fromDateEdit->date(), $toDateEdit->date(), $orderBy, $groupBy);

		$this->assign('operator', $opWork->operator());
		$this->assign('opWorkOperations',  $opWork->operations());
		$this->assign('opWorkTotal',  $opWork->total());


/*
		$listModels = JunObjectsFactory::makeModels();
		$listOperations = array();
		foreach($listModels as $mod) {
			//echo $mod->id()."<br />";
			$opis = JunObjectsFactory::makeOperations($mod->id());
			$listOperations[$mod->id()] = $opis[0];
		}

		//r_print($listOperations);
		//exit;

		// Init Models and Operations
		$this->assign('listModels',  $listModels);
		$this->assign('listOperations',  $listOperations);
		//print_r($listOperations);

		//$page->addJsCode($this->jsfuncInitModels($listModels, $listOperations));
*/
		if($pager)
			$this->assign('pager', $pager->show());
		else
			$this->assign('pager', '');

		$ms = VsMessageStack::Instance();
		$messages = $ms->messages('JunOperatorWork');
		//print_r($messages);
	}
	//===================================================================================================================

	function show() {
		$this->assign('wwwDir', $this->_wwwDir);

		return $this->fetch($this->_template);
	}
	//===================================================================================================================

	private function jsfuncInitModels($models, $operations) {
		$js = "// BEGIN Init Models and Operations\n";
		$js .= "var models = new Array();\n";
		$js .= "var operations = new Array();\n";
		//$js .= "var currentPrices = new Array();\n";
		$js .= "\n";
		foreach($models as $j => $mod) {
			$js .= "models[".$j."] = new Array('".$mod->number()."', '".$mod->name()."');\n";
		}
		$js .= "\n";
		foreach($operations as $k => $ops) {
			$js .= "operations[".$k."] = new Array();\n";
			foreach($ops as $i => $op) {
				$js .= "operations[".$k."][".$i."] = new Array('".$op->id()."', '".$op->number()."', '".$op->name()."', '".$op->price()."');\n";
			}
			$js .= "\n";
		}
		$js .= "\n";
		$js .= "//alert(operations[1].length);\n";
		$js .= "\n";
		$js .= "// END Init Models and Operations\n";
		$js .= "\n";
		$js .= "\n";

		return $js;
	}
	//=====================================================================================================================

	private function jsfuncFormWorksSubmited($models, $operations) {
		$js = "// BEGIN Init Models and Operations\n";
		$js .= "var models = new Array();\n";
		$js .= "var operations = new Array();\n";
		//$js .= "var currentPrices = new Array();\n";
		$js .= "\n";
		foreach($models as $j => $mod) {
			$js .= "models[".$j."] = new Array('".$mod->number()."', '".$mod->name()."');\n";
		}
		$js .= "\n";
		foreach($operations as $k => $ops) {
			$js .= "operations[".$k."] = new Array();\n";
			foreach($ops as $i => $op) {
				$js .= "operations[".$k."][".$i."] = new Array('".$op->id()."', '".$op->number()."', '".$op->name()."', '".$op->price()."');\n";
			}
			$js .= "\n";
		}
		$js .= "\n";
		$js .= "//alert(operations[1].length);\n";
		$js .= "\n";
		$js .= "// END Init Models and Operations\n";
		$js .= "\n";
		$js .= "\n";

		return $js;
	}
	//=====================================================================================================================


	private function procForm() {
		//print_r($_POST['posted_data']); print_r($_POST['work_ids']); exit;
		
		foreach($_POST['work_ids'] as $id=>$value) {
			JunOperatorWork::updateRow($id, $_POST['posted_data'][$id]);
		}

		//exit;
		$url = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
		$url .= "&fromVsdeDate=".$_REQUEST['fromVsdeDate']."&toVsdeDate=".$_REQUEST['toVsdeDate'];
		header("Location: ".$url);

	}
	//=====================================================================================================================


};

?>
